package com.dcbeida.activity.sign;


import android.app.Activity;
import android.content.DialogInterface;
import android.support.annotation.Nullable;

import com.dcbeida.MyApplication;
import com.dcbeida.activity.sales.StoresalesContract;
import com.dcbeida.bean.Drug;
import com.dcbeida.bean.DrugInfo;
import com.dcbeida.bean.DrugListBean;
import com.dcbeida.bean.Patient;
import com.dcbeida.bean.PatientList;
import com.dcbeida.bean.Result_Api;
import com.dcbeida.bean.SignInfo;
import com.dcbeida.bean.SignstatusBean;
import com.dcbeida.callback.DialogCallback;
import com.dcbeida.model.InventoryModel;
import com.dcbeida.model.SignModel;
import com.dcbeida.model.SotresalesModel;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by hp on 2016/5/12.
 */
public class SignPresenter implements SignContract.Present {


    private SignContract.View view;


    private SignModel model;

    private SotresalesModel sotresalesModel;


    private Activity act;

    public SignPresenter(SignContract.View view, Activity act) {
        //我这里直接把activity作为view，所以不需要
        //mView.setPresenter(this);
        this.view = view;
        model = SignModel.newInstance();
        sotresalesModel = SotresalesModel.newInstance();
        this.act = act;

    }

    @Override
    public void start() {

    }

    //取消dialog 取消请求
    DialogInterface.OnCancelListener lis = new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            model.getCancel();
        }
    };


    @Override
    public void getSignQrCode(int patientid) {
        model.getSignQrCode(patientid,new GetdrugcodeCallBack(act, SignInfo.class,lis));
    }

    @Override
    public void queryPatient(int patientid) {
        sotresalesModel.querypatient(4,patientid+"",new QuerypatientCallBack(act,PatientList.class,lis));
    }

    @Override
    public void rejectSign(int patientid) {
        model.rejectSign(patientid,new RejectSignPatientCallBack(act,SignstatusBean.class,lis));
    }

    //药店销售-查询患者信息列表
    class QuerypatientCallBack<T> extends DialogCallback<T> {

        public QuerypatientCallBack(Activity activity, Type type, DialogInterface.OnCancelListener listener) {
            super(activity, type, listener);
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                if (data instanceof PatientList) {
                    if (((PatientList) data).getList().size() > 0) {
                        Patient patient = ((PatientList) data).getList().get(0);
                        if(patient.getSignstatus() == 1){
                            view.showSignSuccess();
                        }else if(patient.getSignstatus() == 2){
                            view.showTip("该患者已拒签");
                        }else if(patient.getSignstatus() == 0){
                            view.showTip("该患者未签署");
                        }
                    } else {
                        view.showTip("未查询到患者信息...");
                    }
                }
            }
        }

        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.showTip("数据未请求成功,请检查网络...");


        }
    }

    class RejectSignPatientCallBack<T> extends DialogCallback<T> {

        public RejectSignPatientCallBack(Activity activity, Type type, DialogInterface.OnCancelListener listener) {
            super(activity, type, listener);
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                if (data instanceof SignstatusBean) {
                   view.showSignStatus(((SignstatusBean) data).getSignstatus());
                }
            }
        }

        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.showTip("数据未请求成功,请检查网络...");


        }
    }


    //根据条形码查询详细信息
    class GetdrugcodeCallBack<T> extends DialogCallback<T> {

        public GetdrugcodeCallBack(Activity activity, Type type, DialogInterface.OnCancelListener listener) {
            super(activity, type, listener);
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                view.showQrCode((SignInfo) data);
            } else {
                view.showTip("没有查询到二维码信息");
            }
        }


        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.showTip("数据未请求成功,请检查网络...");


        }
    }



}
